'''
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with self work for additional information
 * regarding copyright ownership.  The ASF licenses self file
 * to you under the Apache License, Version 2.0 (the
 * "License") you may not use self file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http:#www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
'''
from base.datatypes.xsd.XSDDatatype import XSDDatatype
from base.datatypes.xsd.XSDDatatypeInstances import XSDDatatypeInstances
from base.datatypes.xsd.impl.RDFLangString import rdfLangString

from base.graph.impl.LiteralLabelImpl import LiteralLabelImpl


#import org.apache.jena.JenaRuntime;
#import org.apache.jena.datatypes.DatatypeFormatException;
#import org.apache.jena.datatypes.RDFDatatype;
#import org.apache.jena.datatypes.xsd.XSDDatatype;
#import org.apache.jena.graph.NodeFactory;
#import org.apache.jena.vocabulary.RDF;

class LiteralLabelFactory:
    # This code works for RDF 1.0 and RDF 1.1
    #
    # In RDF 1.0, "abc" has no datatype and is a different term to "abc"^^xsd:string
    # In RDF 1.0, "abc"@en has no datatype.
    #
    # In RDF 1.1, "abc" has no datatype xsd:string and is the same term as "abc"^^xsd:string
    # In RDF 1.1, "abc"@en has datatype rdf:langString.

    # param )
    # result static final RDFDatatype dtSLangString =
    # dtSLangString = NodeFactory.getType(rdfLangString.uri)

    # param RDFDatatype dtype, String lang
    # result static RDFDatatype
    @staticmethod
    def fixDatatype(dtype, lang):
        if (dtype is not None):
            return dtype
        #if (JenaRuntime.isRDF11):
        from base.graph.NodeFactory import NodeFactory
        dtSLangString = NodeFactory.getType(rdfLangString.uri)
        dtype = XSDDatatypeInstances.XSDstring if (lang is None or lang == "") else dtSLangString
        return dtype
    

    '''
     * Create a literal with a dtype.
    '''
    # param String lex, RDFDatatype dtype
    # result static LiteralLabel
    @staticmethod
    def create(lex, dtype):
        return LiteralLabelImpl(lex, "", dtype, None, True)
    

    '''
     * Using:@link #create(String, String)or:@link #create(String, RDFDatatype)
     * where possible is preferred.
    '''
    # param String lex, String lang, RDFDatatype dtype
    # result static LiteralLabel
    # throws DatatypeFormatException:
    @staticmethod
    def createLiteralLabel(lex, lang, dtype):
        dtype = LiteralLabelFactory.fixDatatype(dtype, lang)
        return LiteralLabelImpl(lex, "", dtype, lang, True)
    

    '''
     * Build a plain literal label from its lexical form and language tag.
     *
     * @param lex  the lexical form of the literal
     * @param lang the optional language tag, only relevant for plain literals
    '''
    # param String lex, String lang
    # result static LiteralLabel
    # @staticmethod
    # def create(lex, lang):
    #     #RDFDatatype
    #     dt = LiteralLabelFactory.fixDatatype(None, lang)
    #     return LiteralLabelImpl(lex, lang, dt)
    

    '''
     * Build a typed literal label from its value form. If the value is a string we
     * assume self is intended to be a lexical form after all.
     *
     * @param value the value of the literal
     * @param lang  the optional language tag, only relevant for plain literals
     * @param dtype the type of the literal, null for old style "plain" literals (which become xsd:string in RDF 1.1)
    '''
    # param Object value, String lang, RDFDatatype dtype
    # result static LiteralLabel
    @staticmethod
    def createByValue(value, lang, dtype):
        dtype = LiteralLabelFactory.fixDatatype(dtype, lang)
        return LiteralLabelImpl(None, value, dtype, lang, True)
    

    '''
     * Build a typed literal label from its value form using
     * whatever datatype is currently registered as the default
     * representation for self java class. No language tag is supplied.
     * A plain string becomes an xsd:string.
     *
     * @param value the literal value to encapsulate
    '''
    # param Object value
    # result static LiteralLabel
    @staticmethod
    def createTypedLiteral(value):
        return LiteralLabelImpl(None, value, None, None, True)
    

    '''
     * Creates either a plain literal or an XMLLiteral.
     *
     * @param xml If True then s is exclusive canonical XML of type rdf:XMLLiteral, and no checking will be invoked.
    '''
    # param String s, String lang, boolean xml
    # result static LiteralLabel
    # def create(s, lang, xml):
    #     if (xml):
    #         return LiteralLabelImpl(s, lang, xml)
    #     return create(s, lang)
    



